package com.huopaolan.yohuoshop.Mvp.View.Fragment;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.huopaolan.lib_core.Base.BaseFragment;
import com.huopaolan.yohuoshop.Adapter.BrandAdapter;
import com.huopaolan.yohuoshop.Adapter.BrandHorizontalAdapter;
import com.huopaolan.yohuoshop.Adapter.BrandListAdapter;
import com.huopaolan.yohuoshop.Bean.BrandEntity;
import com.huopaolan.yohuoshop.Bean.BrandListEntity;
import com.huopaolan.yohuoshop.Di.Compont.DaggerBrandFragmentCompont;
import com.huopaolan.yohuoshop.Di.Modules.BrandFragmentModules;
import com.huopaolan.yohuoshop.Listener.HomeBannerImageLoader;
import com.huopaolan.yohuoshop.Mvc.Model.LetterBar;
import com.huopaolan.yohuoshop.Mvc.Model.LetterBar.OnTouchLetterChangeListener;
import com.huopaolan.yohuoshop.Mvp.Contract.BrandFragmentContract;
import com.huopaolan.yohuoshop.Mvp.Presenter.BrandFragmentPresenter;
import com.huopaolan.yohuoshop.NetWork.API;
import com.huopaolan.yohuoshop.R;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BrandFragment extends BaseFragment<BrandFragmentPresenter> implements BrandFragmentContract.BrandIView, OnTouchLetterChangeListener, OnClickListener {
    private RecyclerView brand_recycler, recycler_horizontal;
    private View search_head;
    private EditText search_edit;
    private Banner brand_banner;
    private BrandListAdapter brandListAdapter;
    private LetterBar letterBar;
    private BrandAdapter brandAdapter;
    private BrandHorizontalAdapter horizontalAdapter;
    private RadioButton all_brand, new_brand, hot_brand;
    List<String> letteres;
    //二级菜单列表集合
    List<BrandListEntity> brandListEntities = new ArrayList<>();
    //新入驻品牌集合
    List<BrandEntity.Values> newBrandEntities = new ArrayList<>();
    //热门品牌集合
    List<BrandEntity.Values> hotBrandEntities = new ArrayList<>();
    //推荐品牌集合
    List<BrandEntity.Values> recommendEntities;
    //广告集合
    List<BrandEntity.Values> bannerEntities;

    @Override
    public View createView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.brand_list_layout, null);
    }

    @Override
    public void initView(View view) {
        //初始化控件
        brand_recycler = view.findViewById(R.id.brand_list_recycler);
        letterBar = view.findViewById(R.id.letter_bar);
        letterBar.setOnTouchLetterChangeListener(this);
        //头布局
        search_head = View.inflate(getActivity(), R.layout.search_brand_layout, null);
        all_brand = search_head.findViewById(R.id.all_brand);
        new_brand = search_head.findViewById(R.id.new_brand);
        hot_brand = search_head.findViewById(R.id.hot_brand);
        all_brand.setOnClickListener(this);
        new_brand.setOnClickListener(this);
        hot_brand.setOnClickListener(this);
        search_edit = search_head.findViewById(R.id.search_edit_text);
        recycler_horizontal = search_head.findViewById(R.id.brand_recycler_horizontal);
        brand_banner = search_head.findViewById(R.id.brand_banner_img);
        //添加布局
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        brand_recycler.setLayoutManager(layoutManager);
        brandAdapter = new BrandAdapter(R.layout.brand_list_layout);
        brand_recycler.setAdapter(brandAdapter);
        //添加适配器
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        brand_recycler.setLayoutManager(manager);
        brandListAdapter = new BrandListAdapter(brandListEntities);
        brandListAdapter.addHeaderView(search_head, 0);
        brand_recycler.setAdapter(brandListAdapter);
        //品牌图标适配器
        LinearLayoutManager horizontalManager = new LinearLayoutManager(getActivity());
        horizontalManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recycler_horizontal.setLayoutManager(horizontalManager);
        horizontalAdapter = new BrandHorizontalAdapter(R.layout.brand_recommend_list);
        recycler_horizontal.setAdapter(horizontalAdapter);
    }

    @Override
    public void initData() {
        p.brandRequest();
    }

    @Override
    public void onStart() {
        Window window = getActivity().getWindow();
        window.setStatusBarColor(Color.BLACK);
        super.onStart();
    }

    @Override
    public void setUpDagger() {
        DaggerBrandFragmentCompont.builder()
                .brandFragmentModules(new BrandFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void brandSuccess(Map<String, List> entity) {
        newBrandEntities.addAll(entity.get("recommendEntitys"));
        Toast.makeText(getActivity(), "" + newBrandEntities.get(0).brand_bg, Toast.LENGTH_SHORT).show();
        brandListEntities.addAll(entity.get("brandListEntities"));
        hotBrandEntities.addAll(entity.get("hotBrandEntitys"));
        brandListAdapter.setNewData(brandListEntities);
        horizontalAdapter.addData(newBrandEntities);
        List<String> banners = new ArrayList<>();
        for (int i = 0; i < newBrandEntities.size(); i++) {
            banners.add(API.BaseUrl + newBrandEntities.get(i).brand_bg);
        }
        brand_banner.setImages(banners);
        brand_banner.isAutoPlay(true);
        brand_banner.setIndicatorGravity(BannerConfig.CENTER);
        brand_banner.setDelayTime(1 * 1500);
        brand_banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        brand_banner.setImageLoader(new HomeBannerImageLoader());
        brand_banner.start();
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void onTouchLetterChange(boolean isTouch, String letter) {
        for (int i = 0; i < brandListEntities.size(); i++) {
            if (brandListEntities.get(i).getItemType() == 1) {
                if (brandListEntities.get(i).brand_letter.equalsIgnoreCase(letter)) {
                    Log.d("Letter", "" + brandListEntities.get(i).brand_letter);
                    brand_recycler.scrollToPosition(i + 1);
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.all_brand:
                break;
            case R.id.new_brand:
                break;
            case R.id.hot_brand:
                break;
        }
    }
}
